home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / HUGEAR.ZIP / HALLOCTR.H next >
Encoding:
C/C++ Source or Header  |  1994-11-28  |  5.9 KB  |  198 lines

  1. /*--------------------------------------------------------------------*/
  2. /*                                                                    */
  3. /*  HALLOCTR.H                                                        */
  4. /*                                                                    */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. #if !defined( HALLOCTR_H )
  8. #define HALLOCTR_H
  9.  
  10. #if defined(__DPMI32__) || defined(__WIN32__)
  11. #error Can not use huge allocation in a 32 bit model
  12. #endif
  13.  
  14. #if !defined( __STDLIB_H )
  15. #include <stdlib.h>
  16. #endif  // __STDLIB_H
  17.  
  18. #if !defined( __DOS_H )
  19. #include <dos.h>
  20. #endif  // __DOS_H
  21.  
  22. #if !defined( __ALLOC_H )
  23. #include <alloc.h>
  24. #endif  // __ALLOC_H
  25.  
  26. #if !defined( __EXCEPT_H )
  27. #include <except.h>
  28. #endif  // __EXCEPT_H
  29.  
  30. #if !defined( __CLASSLIB_DEFS_H )
  31. #include "classlib\defs.h"
  32. #endif  // __CLASSLIB_DEFS_H
  33.  
  34. #if defined( BI_CLASSLIB_NO_po )
  35. #pragma option -po-
  36. #endif
  37.  
  38. class _BIDSCLASS THugeXAllocation
  39. {
  40. public:
  41.     THugeXAllocation(unsigned long sz) : size(sz)
  42.       {
  43.       }
  44.  
  45.     unsigned long size;
  46. };
  47.  
  48. /*------------------------------------------------------------------------*/
  49. /*                                                                        */
  50. /*  class THugeStandardAllocator                                          */
  51. /*                                                                        */
  52. /*  Provides class-specific farmalloc and farfree that                    */
  53. /*  simply call the global farmalloc and farfree functions.   That        */
  54. /*  is, THugeStandardAllocator does not provide any specialized behavior. */
  55. /*  It is used in the non-managed versions of the parametrized            */
  56. /*  containers.                                                           */
  57. /*                                                                        */
  58. /*------------------------------------------------------------------------*/
  59.  
  60. class _BIDSCLASS THugeStandardAllocator
  61. {
  62. public:
  63.     void __huge * farmalloc(unsigned long sz)
  64.         {
  65.         void __huge * temp = (void __huge *) ::farmalloc( sz );
  66.         if (!temp)
  67.           {
  68.           throw(THugeXAllocation(sz));
  69.           }
  70.         return (temp);
  71.         }
  72.     void farfree( void __huge *ptr )
  73.         { ::farfree( (void __far *)ptr ); }
  74. };
  75.  
  76. /*------------------------------------------------------------------------*/
  77. /*                                                                        */
  78. /*  class THugeSharedAllocator                                            */
  79. /*                                                                        */
  80. /*  Provides class-specific farmalloc and farfree that                    */
  81. /*  allocate from shared memory.                                          */
  82. /*                                                                        */
  83. /*------------------------------------------------------------------------*/
  84.  
  85. class _BIDSCLASS THugeSharedAllocator
  86. {
  87. public:
  88.     void __huge * farmalloc(unsigned long sz);
  89.     void farfree( void __huge *ptr );
  90. };
  91.  
  92. /*------------------------------------------------------------------------*/
  93. /*                                                                        */
  94. /*  template <class T, class Alloc> class THugeManaged_T                  */
  95. /*                                                                        */
  96. /*  Provides a parametrized wrapper for type T which uses a               */
  97. /*  class-specific farmalloc and farfree functions supplied               */
  98. /*  by Alloc.                                                             */
  99. /*                                                                        */
  100. /*------------------------------------------------------------------------*/
  101.  
  102. template <class T, class Alloc> class THugeManaged_T : private Alloc
  103. {
  104.  
  105. public:
  106.  
  107.     THugeManaged_T() {}
  108.     THugeManaged_T( const T& t ) : data(t)
  109.         {
  110.         }
  111.  
  112.     const THugeManaged_T& operator = ( const THugeManaged_T& t )
  113.         {
  114.         data = t.data;
  115.         return *this;
  116.         }
  117.  
  118.     operator T&()
  119.         {
  120.         return data;
  121.         }
  122.  
  123.     Alloc::farmalloc;
  124.     Alloc::farfree;
  125.  
  126. private:
  127.  
  128.     T data;
  129.  
  130. };
  131.  
  132. /*------------------------------------------------------------------------*/
  133. /*                                                                        */
  134. /*  THugeSharedAllocator::farmalloc                                       */
  135. /*  THugeSharedAllocator::farfree                                         */
  136. /*                                                                        */
  137. /*  When compiling for WINDOWS, allocates memory as GMEM_DDESHARE.        */
  138. /*  When compiling for DOS, uses the global farmalloc and                 */
  139. /*  farfree functions.                                                    */
  140. /*                                                                        */
  141. /*------------------------------------------------------------------------*/
  142.  
  143. #if defined( _Windows )
  144.  
  145. #include <windows.h>
  146.  
  147. inline void __huge * THugeSharedAllocator::farmalloc(unsigned long sz)
  148.  
  149.   {
  150.  
  151.   HGLOBAL hMem = GlobalAlloc( GPTR | GMEM_DDESHARE, DWORD(sz) );
  152.  
  153.   if (hMem == NULL)
  154.     {
  155.     throw(THugeXAllocation(sz));
  156.     }
  157.   return ((void __huge *)GlobalLock( hMem ));
  158.   }
  159.  
  160. inline void THugeSharedAllocator::farfree( void __huge *ptr )
  161.  
  162.   {
  163.  
  164.   HGLOBAL hMem = (HGLOBAL)GlobalHandle(FP_SEG((void __far *)ptr));
  165.  
  166.   if (GlobalUnlock(hMem))
  167.       GlobalFree(hMem);
  168.   }
  169.  
  170. #else
  171.  
  172. inline void __huge * THugeSharedAllocator::farmalloc(unsigned long sz)
  173.  
  174.   {
  175.  
  176.   void __huge * temp = (void __huge *) ::farmalloc( sz );
  177.  
  178.   if (!temp)
  179.     {
  180.     throw(THugeXAllocation(sz));
  181.     }
  182.   return (temp);
  183.   }
  184.  
  185. inline void THugeSharedAllocator::farfree( void __huge *ptr )
  186.  
  187.   {
  188.   ::farfree( (void __far *)ptr );
  189.   }
  190.  
  191. #endif  // __WINDOWS__
  192.  
  193. #if defined( BI_CLASSLIB_NO_po )
  194. #pragma option -po.
  195. #endif
  196.  
  197. #endif  // HALLOCTR_H
  198.